package api

import (
	"github.com/LyricTian/gin-admin/v7/internal/app/bll"
	"github.com/LyricTian/gin-admin/v7/internal/app/ginx"
	"github.com/LyricTian/gin-admin/v7/internal/app/schema"
	"github.com/gin-gonic/gin"
	"github.com/google/wire"
)

// LikeSet 注入Like 点赞收藏管理
var LikeSet = wire.NewSet(wire.Struct(new(Like), "*"))

// Like 点赞收藏管理
type Like struct {
	LikeBll *bll.Like
}

// Query 查询数据
func (a *Like) Query(c *gin.Context) {
	ctx := c.Request.Context()
	var params schema.LikeQueryParam
	if err := ginx.ParseQuery(c, &params); err != nil {
		ginx.ResError(c, err)
		return
	}

	params.Pagination = true
	result, err := a.LikeBll.Query(ctx, params)
	if err != nil {
		ginx.ResError(c, err)
		return
	}

	ginx.ResPage(c, result.Data, result.PageResult)
}

// Get 查询指定数据
func (a *Like) Get(c *gin.Context) {
	ctx := c.Request.Context()
	item, err := a.LikeBll.Get(ctx, c.Param("id"))
	if err != nil {
		ginx.ResError(c, err)
		return
	}
	ginx.ResSuccess(c, item)
}

// Create 创建数据
func (a *Like) Create(c *gin.Context) {
	ctx := c.Request.Context()
	var item schema.Like
	if err := ginx.ParseJSON(c, &item); err != nil {
		ginx.ResError(c, err)
		return
	}
	item.Creator = ginx.GetUserID(c)
	result, err := a.LikeBll.Create(ctx, item)
	if err != nil {
		ginx.ResError(c, err)
		return
	}

	ginx.ResSuccess(c, schema.SetLikeResult{LikeNum:result})
}

// Update 更新数据
func (a *Like) Update(c *gin.Context) {
	ctx := c.Request.Context()
	var item schema.LikeUpdate
	if err := ginx.ParseJSON(c, &item); err != nil {
		ginx.ResError(c, err)
		return
	}

	err := a.LikeBll.Update(ctx, c.Param("id"), *item.ToLike())
	if err != nil {
		ginx.ResError(c, err)
		return
	}
	ginx.ResOK(c)
}

// Delete 删除数据
func (a *Like) Delete(c *gin.Context) {
	ctx := c.Request.Context()
	err := a.LikeBll.Delete(ctx, c.Param("id"))
	if err != nil {
		ginx.ResError(c, err)
		return
	}
	ginx.ResOK(c)
}
